﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using POSPlus.EZBarTender;
using Seagull.BarTender.Print;
using System.Windows.Forms;

namespace POSPlus.PrintManager
{
	public class SignsWriter : EzbtWriter
	{
		private string _target;
		private List<PrintJob> _jobs;
		private string _connectionString;
		private string _labelLocation;
		private List<SignsWithJobName> _info;

		public override string Target
		{
			get
			{
				return _target;
			}
			set
			{
				_target = value;
			}
		}
		public override List<PrintJob> JobName
		{
			get
			{
				return _jobs;
			}
			set
			{
				_jobs = value;
			}
		}
		public override string ConnectionString
		{
			get
			{
				return _connectionString;
			}
			set
			{
				_connectionString = value;
			}
		}
		public string LabelLocation
		{
			get { return _labelLocation; }
			set { _labelLocation = value; }
		}
		public List<SignsWithJobName> PrintInformation
		{
			get { return _info; }
			set { _info = value; }
		}

		public SignsWriter(string connectionString, string labelLocation, List<PrintJob> jobs, string target)
		{
			ConnectionString = connectionString;
			LabelLocation = labelLocation;
			JobName = jobs;
			Target = target;
		}

		public override void Write()
		{
			BarTenderTreeBuilder builder = new BarTenderTreeBuilder(Target, "");
			builder.LabelLocation = LabelLocation;
			builder.StartTextData();
			string fields = "\"Description1\",\"Description2\",\"Description3\",\"Description4\",\"Description5\"," +
				"\"Selling\",\"UnitSP\",\"Size\",\"Uom\",\"NormalPrice\",\"YouSave\",\"WeekEnding\",\"StartDate\"\n";
			builder.AppendTextData(fields);
			bool errors = false;
			for (int i = 0; i < _info.Count; i++)
			{
				for (int j = 0; j < _info[i].Copies; j++)
				{
					try
					{
						SignsWithJobName info = _info[i];
						string data = "";
						data += "\"" + info.Description1 + "\",";
						data += "\"" + info.Description2 + "\",";
						data += "\"" + info.Description3 + "\",";
						data += "\"" + info.Description4 + "\",";
						data += "\"" + info.Description5 + "\",";
						data += "\"" + Selling(info.SpecialPrice) + "\",";
						data += "\"" + UnitNP(info.UnitSP) + "\",";
						data += "\"" + info.Size + "\",";
                        data += "\"" + info.Uom + "\",";//(info.Size.Split(' ').Length > 1 ? info.Size.Split(' ')[1] : "") + "\",";
						data += "\"" + ConvertMoney(info.NormalPrice) + "\",";
						data += "\"" + ConvertMoney(info.YouSave) + "\",";
						data += "\"" + info.WeekEnding.ToString("MM/dd/yyyy") + "\",";
                        data += "\"" + info.StartDate.ToString("MM/dd/yyyy") + "\"";

						if (j + 1 < _info[i].Copies || i + 1 < _info.Count)
							data += "\n";

						builder.AppendTextData(data);
					}
					catch
					{
						errors = true;
					}
				}
			}

			builder.CloseTextData();

			using (Engine engine = new Engine(true))
			{
				Messages msgs = null;
				engine.XMLScript(Target, XMLSourceType.ScriptFile, out msgs);

				if (errors)
				{
                    if (msgs != null)
                    {
                        Seagull.BarTender.Print.Message message = new Seagull.BarTender.Print.Message(
                            "Some signs were skipped due to errors",
                            13, MessageSeverity.Error, "Sign Printing");
                        msgs.Add(message);
                    }
				}

				foreach (Seagull.BarTender.Print.Message msg in msgs)
				{
					MessageBox.Show(msg.Text, msg.Source.ToString());
				}
				engine.Stop();
			}
		}

		private string SellingPt3(string specialPrice)
		{
			if (specialPrice.Split('/').Length > 1)
				return specialPrice.Split('/')[1];

			return "";
		}

		private string SellingPt2(string specialPrice)
		{
			if (specialPrice.Split('/').Length > 1)
				return "$";

			return "";
		}

		private string SellingPt1(string specialPrice)
		{
			if (specialPrice.Split('/').Length > 1)
			{
				return specialPrice.Split('/')[0] + "/";
			}

			return "";
		}

		private string Selling(string specialPrice)
		{
			try
			{
				if (specialPrice.Split('/').Length == 1)
				{
					if (specialPrice.Contains("\u00A2"))
						return ConvertMoney(double.Parse(specialPrice.Substring(0, 2)) / 100);
					else return ConvertMoney(double.Parse(specialPrice));
				}
				else if (specialPrice.Split('/').Length == 2)
				{
					string[] split = specialPrice.Split('/');
					string returnString = split[0] + "/";
					returnString += ConvertMoney(double.Parse(split[1]));
					return returnString;
				}
			}
			catch
			{
				throw;
			}

			return "";
		}

		public override void GetInfo()
		{
			_info = new List<SignsWithJobName>();
			foreach (PrintJob job in _jobs)
			{
				_info.AddRange(SignsWithJobName.GetAllOnJobName(ConnectionString, job.JobName, job.Type));
			}
			_info.Sort();
		}
	}
}
